package com.ganzi.ganziserver.service.imp;

import com.ganzi.ganziserver.core.*;
import com.ganzi.ganziserver.dao.*;
import com.ganzi.ganziserver.service.SchoolInformationService;
import com.ganzi.ganziserver.utils.GetTime;
import com.ganzi.ganziserver.utils.OtherException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SchoolInformationServiceImp implements SchoolInformationService{

    private static Logger log = LoggerFactory.getLogger(SchoolInformationServiceImp.class);

    @Autowired
    UserDao userDao;
    @Autowired
    SchoolInformationDao infoDao;
    @Autowired
    FanInfromationDao fanDao;
    @Autowired
    FanCollectDao collectDao;
    @Autowired
    FanStateDao stateDao;

    @Override
    public List<SchoolInformation> findAllSchoolInfoemation() {
        try{
            List<SchoolInformation> list = infoDao.findAllSchoolInformation();
            return list;
        }catch (Exception e){
            log.info("查询学校信息出错："+e.getMessage());
            return null;
        }
    }

    @Override
    public SchoolInformation findSchoolInfoemationById(long sid) {
        try{
            SchoolInformation info = infoDao.findSchoolInformationById(sid);
            return info;
        }catch (Exception e){
            log.info("查询学校信息出错："+e.getMessage());
            return null;
        }
    }

    @Override
    public List<Long> findSchoolByUserId(long uid) {
        try{
            List<Long> list = userDao.findSchoolByUserId(uid);
            return list;
        }catch (Exception e){
            log.info("查询用户与学校关系出错："+e.getMessage());
            return null;
        }
    }

    @Override
    public List<FanInformation> findFanBySchoolId(long sid) {
        try{
            List<FanInformation> list = fanDao.findFanInformationBySchool(sid);
            return list;
        }catch (Exception e){
            log.info("查询盘管信息异常："+e.getMessage());
            return null;
        }
    }

    @Transactional
    @Override
    public void addFan(FanInformation fan) throws Exception {
        if(fan != null){
            Long m = fanDao.findFanInformationBySchoolAndCollectAndNumber(fan.getSchoolId(), fan.getCollectId(), fan.getFanNumber());
            if(m != null)throw new OtherException("盘管已经存在");
            fanDao.insertFanInformation(fan);
            if(fan.getId() != 0){
                FanState state = FanState.builder().fanId(fan.getId()).isOnLine(0).updateTime(GetTime.getNowTime()).build();
                stateDao.insertFanState(state);
            }
        }
    }

    @Override
    public void updateFan(FanInformation fan) throws Exception {
        if(fan != null)fanDao.updateFanInformation(fan);
    }

    @Override
    public void addSchool(SchoolInformation school) throws Exception {
        if(school != null){
            SchoolInformation number = infoDao.findSchoolInformationBynumber(school.getSchoolNumber());
            if(number != null)throw new OtherException("学校已经存在");
            Long sid = infoDao.findSchoolIdByQueue(school.getChannelName());
            if(sid != null)throw new OtherException("学校已经存在");
           infoDao.insertSchoolInformation(school);
        }
    }

    @Override
    public void updateSchool(SchoolInformation school) throws Exception {
        if(school != null)infoDao.updateSchoolInformation(school);
    }

    @Override
    public void addCollection(FanCollect collect) throws Exception {
        if(collect != null){
            Long m = collectDao.findFanCollectIdByschoolIdAndNumber(collect.getSchoolId(), collect.getCollectNumber());
            if( m != null)throw new OtherException("采集器已经存在");
            collectDao.insertFanCollect(collect);
        }
    }

    @Override
    public List<Long> getAllSchoolId() throws Exception {
        return infoDao.findAllSchoolId();
    }

    @Override
    public List<FanCollect> findCollection(int state)throws Exception {
        return collectDao.findFanCollectBystate(state);
    }

    @Override
    public int findCollectionCount(int state) throws Exception {
        return collectDao.findFanCollectBystateCount(state);
    }


    @Override
    public List<TempSchool> getAllSchoolById(long id) throws Exception {
        return infoDao.findAllSchoolForCollection(id);
    }
}
